﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Utils;
using QFlow.Data;
using QFlow.Core;

namespace QFlow.Windows.Forms
{
    public partial class NewPayment : BaseForm
    {
        bool _Income;
        public NewPayment(QFlowDbContext context, bool income)
            : base(context)
        {
            InitializeComponent();

            cmbAccounts.SelectedIndex = 0;

            if (income)
            {
                lblAccounts.Text = QFlow.Windows.Properties.Resources.lblToAccount;
                _Income = true;
            }
            else _Income = false;
        }

        public NewPayment(QFlowDbContext context, bool income, double amount)
            : base(context)
        {
            InitializeComponent();
            txtAmount.Text = amount.ToString();
            txtAmount.ReadOnly = true;

            cmbAccounts.SelectedIndex = 0;

            if (income)
            {
                lblAccounts.Text = QFlow.Windows.Properties.Resources.lblToAccount;
                _Income = true;
            }
            else _Income = false;
        }

        public NewPayment(QFlowDbContext context, bool income, double amount, Account selectedAccount)
            : base(context)
        {
            InitializeComponent();
            txtAmount.Text = amount.ToString();

            cmbAccounts.SelectedIndex = Context.Accounts.IsCashirAccount(selectedAccount.AccountID) ? 0 : 1;

            if (income)
            {
                lblAccounts.Text = QFlow.Windows.Properties.Resources.lblToAccount;
                _Income = true;
            }
            else _Income = false;
        }

        public bool Income
        {
            get { return _Income; }
        }

        public double Amount
        {
            get
            {
                return double.Parse(txtAmount.Text);
            }
        }

        public Account SelectedAccount
        {
            get
            {
                return cmbAccounts.SelectedIndex == 0 ? Context.Accounts.GetCashirAccount() : Context.Accounts.GetSafeAccount();
            }
        }


        //
        protected override void OnActivated(EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (superValidator1.Validate())
            {
                if (!_Income)
                {
                    if (Amount > SelectedAccount.Amount)
                    {
                        MessageBox.Show(QFlow.Windows.Properties.Resources.MBInsuffecientAmount, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        return;
                    }
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
